home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir30 / dfx23d3.zip / DXF23D3.C next >
C/C++ Source or Header  |  1994-03-27  |  10KB  |  357 lines

  1. /* DXF23D2 */
  2.  
  3. #include    <conio.h>
  4. #include        <stdlib.h>
  5. #include        <stdio.h>
  6. #include        <math.h>
  7. #include        <string.h>
  8.  
  9. #define    MAXPOINTS    1000
  10. #define    MAXFACES    1000
  11. #define    MAXPPF          10               // Max points per face
  12.  
  13.  
  14.  
  15.     FILE    *dxf_in_file,*objfile;
  16. char    *fname;
  17. double    x1; double    y1; double    z1; double    x2; double    y2;
  18. double    z2; double    x3; double    y3; double    z3; double    x4;
  19. double    y4; double    z4;
  20.  
  21. void    save_points(double scale);
  22. void    process_points(void);
  23. int    checkold(int trntc,double tmpx, double tmpy, double tmpz);
  24. void    output3d3(void);
  25.  
  26. main(int argc, char *argv[])
  27. {
  28.  
  29.     fname = argv[2];
  30.     char    str[80];
  31.     int     key, npnts,layer,l;
  32.     double  scale = 1.0;
  33.     scale = atof(argv[4]);            // scale factor
  34.     if(scale <= 0) scale = 1;
  35.  
  36.     if(argc < 3) {
  37.         printf("\nDXF23D3 Version 0.01 - Adapted from Stephen Coy's DXF2V\n");
  38.         printf("Converts 3DFACEs in DXF files to 3D3 files of 3D3 object file data.\n");
  39.         printf("Extracts by Layers (if desired)\n");
  40.         printf("Added code by Tom Barber 4/92  Mike Daigle 7/92\n");
  41.         printf("Modified code to output RAW triangles 7/92 Adam Shiffman\n\n");
  42.     printf("     ╔══════════════════════════════════════════════════════╗\n");
  43.     printf("     ║ Modified & added code to convert to 3D3 object data  ║\n");
  44.     printf("     ║                3/93 Chris Olivier                    ║\n");
  45.     printf("     ╚══════════════════════════════════════════════════════╝\n");
  46.     printf("\nUsage: dxf23d3 file.dxf file.inc <Layer Number> [Scale]\n\n");
  47.         printf("Where:\n");
  48.         printf("       file.dxf = name of dxf file with extension\n");
  49.         printf("       file.inc = name of inc file with extension\n");
  50.         printf("       <Layer #>= Layer to be converted.  Must be specified\n");
  51.         printf("       [Scale]  = Scale factor for points (can be floating point)");
  52.         printf("\n\n Typical: DXF23D3 obj.dxf obj.inc -1 1\n");
  53.         printf("          (convert obj.dxf to obj.inc all layers)\n");
  54.         exit(1);
  55.     }
  56.  
  57.     //Open the files
  58.     if (!(dxf_in_file = fopen(argv[1],"rb")))
  59.         { puts("Can not open DXF file!");
  60.           exit(1);
  61.         }
  62. layer = -1;
  63.  
  64. if (argc > 3) layer = atoi(argv[3]);
  65.  
  66.     while(fgets(str, 80, dxf_in_file)) {
  67.         //Wait until we find a 3DFACE...
  68.         if(strncmp(str, "3DFACE", 6) != 0) {continue;}
  69.            fgets(str,80,dxf_in_file);
  70.            if (atoi(str) != 8) {printf("WARNING...LAYER...MISSED\n");}
  71.            fgets(str,80,dxf_in_file);
  72.  
  73.            if (layer != atoi(str) && layer != -1)   {continue;}
  74.  
  75.         x1 = x2 = x3 = x4 = y1 = y2 = y3 = y4 = z1 = z2 = z3 = z4 = 0.0;
  76.         do {
  77.             fgets(str, 80, dxf_in_file);
  78.             key = atoi(str);
  79.             fgets(str, 80, dxf_in_file);
  80.             switch(key) {
  81.                 case 10 : x1 = atof(str); break;
  82.                 case 20 : y1 = atof(str); break;
  83.                 case 30 : z1 = atof(str); break;
  84.                 case 11 : x2 = atof(str); break;
  85.                 case 21 : y2 = atof(str); break;
  86.                 case 31 : z2 = atof(str); break;
  87.                 case 12 : x3 = atof(str); break;
  88.                 case 22 : y3 = atof(str); break;
  89.                 case 32 : z3 = atof(str); break;
  90.                 case 13 : x4 = atof(str); break;
  91.                 case 23 : y4 = atof(str); break;
  92.                 case 33 : z4 = atof(str); break;
  93.                 default : break;
  94.             }
  95.         } while(key != 33);
  96.  
  97.     save_points(scale);
  98.  
  99.  
  100. //        if(npnts < 3) continue;
  101.    }
  102.  
  103.    process_points();
  104.  
  105. fclose(objfile);
  106. fclose(dxf_in_file);
  107. output3d3();
  108. printf("\nScale = %.4f",scale);
  109. //printf("\n\nPress a key...\n");
  110. //getch();
  111. return    0;
  112. }       /* end of main() */
  113.  
  114. //============================================================================
  115. // SUB: SavePoints
  116. //============================================================================
  117. // Global Point Vars
  118.  
  119.  
  120. int     trnct         =     0;
  121. int    num_faces    =    0;
  122. int    num_points    =    0;
  123. int    this_face    =    0;
  124. int    this_point    =    0;
  125.  
  126. double    xf[MAXPOINTS];
  127. double    yf[MAXPOINTS];
  128. double    zf[MAXPOINTS];
  129.  
  130. int    xi[MAXPOINTS];
  131. int    yi[MAXPOINTS];
  132. int    zi[MAXPOINTS];
  133.  
  134. int     facelist[MAXFACES][MAXPPF];    // [face #][pointlist order]
  135.                     // repeat marks last one (pad w/ 0)
  136. void     save_points(double scale)
  137.  
  138. {
  139.     int    ptsinface = 0;
  140.  
  141.  
  142.     num_faces++;            // Face these points apply to
  143.     this_face++;            // Face we're working on
  144.  
  145.     this_point++;            // current point in list (gross)
  146.     xf[this_point]=x1*scale;
  147.     yf[this_point]=y1*scale;
  148.     zf[this_point]=z1*scale;
  149.     facelist[this_face][1] = this_point;
  150.  
  151.  
  152.     this_point++;            // current point in list (gross)
  153.     xf[this_point]=x2*scale;
  154.     yf[this_point]=y2*scale;
  155.     zf[this_point]=z2*scale;
  156.     facelist[this_face][2] = this_point;
  157.  
  158.  
  159.     this_point++;            // current point in list (gross)
  160.     xf[this_point]=x3*scale;
  161.     yf[this_point]=y3*scale;
  162.     zf[this_point]=z3*scale;
  163.     facelist[this_face][3] = this_point;
  164.  
  165.     this_point++;            // current point in list (gross)
  166.     xf[this_point]=x4*scale;
  167.     yf[this_point]=y4*scale;
  168.     zf[this_point]=z4*scale;
  169.     facelist[this_face][4] = this_point;
  170.  
  171.     facelist[this_face][5] = facelist[this_face][1];
  172.     facelist[this_face][6] = 0;    // padding
  173.  
  174.  
  175. // Check if its a triangle
  176.  
  177.  
  178.     if(x4==x3 && y4==y3 && z4==z3) {
  179.         this_point--;
  180.         ptsinface = 3;           // this is a triangle
  181.         facelist[this_face][4] = facelist[this_face][1];
  182.         facelist[this_face][5] = 0;
  183.     }
  184.     else    ptsinface = 4;
  185.  
  186. // put # of points in face in position 0 of face_list
  187.  
  188.     facelist[this_face][0] = ptsinface;
  189.  
  190.     num_points = num_points+ptsinface;
  191. }
  192. //============================================================================
  193. // SUB: ProcessPoints
  194. //============================================================================
  195.  
  196. void    process_points(void) {
  197. // double    xx = 0.0,yy = 0.0, zz=0.0;
  198.  
  199.     printf("\n\nNumber of points: %d",num_points);
  200.     printf("\nNumber of faces : %d",num_faces);
  201. //----------------------------------------------------------------------------
  202. // TRUNCATE POINTS
  203. //----------------------------------------------------------------------------
  204.  
  205.     double    tmpx,tmpy,tmpz;
  206.     int    point_table[MAXPOINTS];
  207.     int    ptnum = 0;
  208. // Fill Point_table with garbage
  209.     for (int xxxx=0;xxxx<=MAXPOINTS;xxxx++) point_table[xxxx] = 9999;
  210. // Check for repeated points & set pointer to that point in Point_Table
  211.     for(int nnn=1;nnn<=num_points;nnn++)
  212.     {
  213.         tmpx = xf[nnn];
  214.         tmpy = yf[nnn];
  215.         tmpz = zf[nnn];
  216.         if((checkold(trnct,tmpx,tmpy,tmpz)) == 0)  // if not in list...
  217.         {
  218.  
  219.             trnct++;
  220.             xi[trnct] = (xf[nnn] + 0.5);
  221.             yi[trnct] = (yf[nnn] + 0.5);
  222.             zi[trnct] = (zf[nnn] + 0.5);
  223.  
  224.             point_table[nnn] = trnct;
  225.         }
  226.         else
  227.         {
  228.         ptnum = checkold(trnct,tmpx,tmpy,tmpz);
  229.         point_table[nnn] = ptnum;
  230.         }
  231.  
  232.     }
  233.     printf("\n\nNumber of unique points: %d\n",trnct);
  234. //    getch();
  235.  
  236.  
  237.  
  238. //    for(int j=1;j<=trnct;j++)
  239. //    {
  240. //        printf("\nPoint %d : ( %d, %d, %d)",j,xi[j],yi[j],zi[j]);
  241. //    }
  242.  
  243. //    printf("\n\nPress a key...");
  244.  
  245. //    for(j=1;j<=num_points;j++)
  246. //    {
  247. //        printf("\nPoint %d   \t-=>\t Point %d",j,point_table[j]);
  248. //    }
  249.  
  250. //    printf("\n\nPress a key...");
  251. //    getch();
  252. //----------------------------------------------------------------------------
  253. // Now, put the face orders together
  254. //----------------------------------------------------------------------------
  255.     int    n = 0;
  256.     int    k = 0;
  257.     int     j;
  258.     for(j=1;j<=num_faces;j++)
  259.     {
  260.         k=facelist[j][0];    // Number of points for this face
  261.         for(int m=1;m<=k+1;m++)
  262.         {
  263.             n = facelist[j][m];
  264.             facelist[j][m] = point_table[n];
  265.         }
  266.     }
  267. /*
  268.     for(j=1;j<=num_faces;j++)
  269.     {
  270.         printf("\nFace #%d:  (",j);
  271.         for(k=1;k<=facelist[j][0]+1;k++)
  272.         {
  273.             printf(" Pt%d",facelist[j][k]);
  274.         }
  275.         printf(" )");
  276.     }
  277. */
  278. }
  279. //============================================================================
  280. // SUB: Checkold(tmpx,tmpy,tmpz)
  281. //     Check if point is already in list
  282. //      Returns 0 if its not already in the list
  283. //    Returns POINT# if already in list.
  284. //============================================================================
  285. int    checkold(int trnct,double tmpx, double tmpy, double tmpz)
  286. {
  287. double    ttmpx,ttmpy,ttmpz;
  288.  
  289.     for(int nn=1;nn<=trnct;nn++)
  290.     {
  291.         ttmpx=xf[nn];
  292.         ttmpy=yf[nn];
  293.         ttmpz=zf[nn];
  294.         if(ttmpx==tmpx && ttmpy==tmpy && ttmpz==tmpz) return nn;
  295.     }
  296.  
  297. return 0;
  298. }
  299. //============================================================================
  300. // SUB: Output3D3
  301. //    Output 3D3 file
  302. //============================================================================
  303. void    output3d3(void)
  304. {
  305.     if (!(objfile = fopen(fname,"w")))
  306.         { puts("Can not open file for output!");
  307.           fclose(dxf_in_file);
  308.           exit(1);
  309.         }
  310. //----------------------------------------------------------------------------
  311. // File open....Now write header
  312. //----------------------------------------------------------------------------
  313.  
  314. //    fprintf(objfile,"%.4f %.4f %.4f\n\n", x4, y4, z4);
  315.     fprintf(objfile,"; Object data generated by DXF23D3 v0.01 for 3DVECT35");
  316.     fprintf(objfile,"\n\n; Number of unique points = %d",trnct);
  317.     fprintf(objfile,"\nlabel\tdw\t%d",trnct);
  318.     fprintf(objfile,"\n; Number of faces = %d",num_faces);
  319.     fprintf(objfile,"\n\tdw\t%d",num_faces);
  320.     fprintf(objfile,"\n; Furure use words");
  321.     fprintf(objfile,"\n\tdw\t25 dup (0)\n");
  322.     fprintf(objfile,"\ndfltcolr\tequ\t1\n");       // default color
  323. // Start of points loop
  324.     fprintf(objfile,"; POINTS");
  325. //------------------------------+
  326. // Print out points             +
  327. //------------------------------+
  328.  
  329.     for(int j=1;j<=trnct;j++)
  330.     {
  331.         fprintf(objfile,"\n\tdw\t%d, %d, %d\t\t\t; Point %d",xi[j],yi[j],zi[j],j);
  332.     }
  333.  
  334.     fprintf(objfile,"\n\n; FACES\n");
  335.  
  336.     for(j=1;j<=num_faces;j++)
  337.     {
  338. // This is the header for each face...
  339.         fprintf(objfile,"\ndw \t0,shade,0,dfltcolr,0 ");
  340.  
  341.         for(int k=1;k<=facelist[j][0]+1;k++)
  342.         {
  343.             fprintf(objfile,", %d",facelist[j][k]);
  344.         }
  345.  
  346.         fprintf(objfile,"  ,0,0,0\t; Face #%d",j);
  347.  
  348.     }
  349. // Pad the bottom (I hate not being able to cursor past EOF!
  350.     fprintf(objfile,"\n\n\n\n");
  351. // Done.  Close the file
  352.     fclose(objfile);
  353. }
  354. //============================================================================
  355. //============================================================================
  356. //============================================================================
  357.